// +----------------------------------------------------------------------
/**
 * 使用示例：
 * import {DownProgress} from '@/utils/DownProgress.js'
 * onLoad() {
	 let prog=new DownProgress();
	 //创建下载
	 prog.create('#ff0000');
	 //模拟进度[达到100%时自动关闭]
	 let i=0;
	 let times=setInterval(()=>{
	 	i++;
	 	if(prog.progress(i)){
	 		clearInterval(times);
	 	}
	 },100);
	 //强制关闭
	 prog.close();
 }
 */
// +----------------------------------------------------------------------
export class DownProgress{
	// 进条度的宽度
	#progressWidth;
	#view1=null;
	#view2=null;
	#view3=null;
	#view4=null;
	constructor(){
		this.progressWidth=260;
	}
	create(progrColor,progText='正在下载版本...'){
		let screenWidth = plus.screen.resolutionWidth
		let screenHeight = plus.screen.resolutionHeight
		this.view1 = new plus.nativeObj.View('mask-layer-202503201337', {
			top: '0px',
			left: '0px',
			height: '100%',
			width: '100%',
			backgroundColor: 'rgba(0,0,0,0.1)'
		})
		this.view1.show();
		let childWidth=300;
		let childHeight=80;
		let posLeft=(screenWidth-childWidth)/2;
		let posTop=(screenHeight-childHeight)/2;
		this.view2 = new plus.nativeObj.View('mask-layer-202503201337-child', {
			top: posTop+'px',
			left: posLeft+'px',
			height: childHeight+'px',
			width: childWidth+'px'
		});
		this.view2.draw([
			{
				tag: "img",
				id: "child-bg-2515875",		
				src: '/static/fixed/update-app-bg.png',
				position:{top:"0",left:"0",width:(childWidth)+'px',height:(childHeight+0)+'px'}
			},
			{
				tag: "font",
				id: "font-title",		
				text: progText,
				textStyles:{size:'14px',color:'#333'},
				position:{top:"-10px"}
			}
		]);
		this.view2.show();
		childWidth=this.progressWidth;
		childHeight=10;
		posLeft=(screenWidth-childWidth)/2;
		posTop=(screenHeight-childHeight)/2;
		this.view3 = new plus.nativeObj.View('mask-layer-202503201337-child-2', {
			top: (posTop+10)+'px',
			left: posLeft+'px',
			height: childHeight+'px',
			width: childWidth+'px',
			backgroundColor: '#eee'
		});
		this.view3.show();
		this.view4 = new plus.nativeObj.View('mask-layer-202503201337-child-2-1', {
			top: (posTop+10)+'px',
			left: posLeft+'px',
			height: childHeight+'px',
			width: '0px',
			backgroundColor: progrColor
		});
		this.view4.show();
		return this;
	}
	progress(num){
		let showWidth=num*(this.progressWidth/100);
		this.view4.setStyle({width:showWidth+"px"});
		if(showWidth >= (this.progressWidth-1)){
			setTimeout(()=>{
				this.close();
			},2000);
			return true;
		}
		return false;
	}
	close(){
		this.view4.hide();
		this.view4.close();
		this.view3.hide();
		this.view3.close();
		this.view2.hide();
		this.view2.close();
		this.view1.hide();
		this.view1.close();
	}
}